package org.dromara.answer.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.answer.domain.vo.AiOptionsVo;
import org.dromara.answer.domain.bo.AiOptionsBo;
import org.dromara.answer.service.IAiOptionsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 选项
 *
 * @author Lion Li
 * @date 2025-04-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/answer/options")
public class AiOptionsController extends BaseController {

    private final IAiOptionsService aiOptionsService;

    /**
     * 查询选项列表
     */
    @SaCheckPermission("answer:options:list")
    @GetMapping("/list")
    public TableDataInfo<AiOptionsVo> list(AiOptionsBo bo, PageQuery pageQuery) {
        return aiOptionsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出选项列表
     */
    @SaCheckPermission("answer:options:export")
    @Log(title = "选项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AiOptionsBo bo, HttpServletResponse response) {
        List<AiOptionsVo> list = aiOptionsService.queryList(bo);
        ExcelUtil.exportExcel(list, "选项", AiOptionsVo.class, response);
    }

    /**
     * 获取选项详细信息
     *
     * @param optionId 主键
     */
    @SaCheckPermission("answer:options:query")
    @GetMapping("/{optionId}")
    public R<AiOptionsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long optionId) {
        return R.ok(aiOptionsService.queryById(optionId));
    }

    /**
     * 新增选项
     */
    @SaCheckPermission("answer:options:add")
    @Log(title = "选项", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AiOptionsBo bo) {
        return toAjax(aiOptionsService.insertByBo(bo));
    }

    /**
     * 修改选项
     */
    @SaCheckPermission("answer:options:edit")
    @Log(title = "选项", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AiOptionsBo bo) {
        return toAjax(aiOptionsService.updateByBo(bo));
    }

    /**
     * 删除选项
     *
     * @param optionIds 主键串
     */
    @SaCheckPermission("answer:options:remove")
    @Log(title = "选项", businessType = BusinessType.DELETE)
    @DeleteMapping("/{optionIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] optionIds) {
        return toAjax(aiOptionsService.deleteWithValidByIds(List.of(optionIds), true));
    }
}
